package com.itfitness.ftlib.widget.meituannav;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

/**
 * @ProjectName: FitnessLib
 * @Package: com.itfitness.ftlib.widget.meituannav
 * @ClassName: MeiTuanNavView
 * @Description: java类作用描述 仿美团外卖底部导航
 * @Author: 作者名
 * @CreateDate: 2020/11/29 10:04
 * @UpdateUser: 更新者：itfitness
 * @UpdateDate: 2020/11/29 10:04
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MeiTuanNavView extends LinearLayout {

    private LayoutParams itemLayoutParams;//item的layoutparams

    public MeiTuanNavView(Context context) {
        this(context,null);
    }

    public MeiTuanNavView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MeiTuanNavView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        itemLayoutParams = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        itemLayoutParams.weight = 1;
        itemLayoutParams.gravity = Gravity.BOTTOM;
    }

    public void setNavDatas(@NonNull List<NavBean> navDatas,int defaultPos){
        removeAllViews();
        for(int i = 0 ; i < navDatas.size() ; i++){
            NavBean navBean = navDatas.get(i);
            final MeiTuanNavViewItem navViewItem = new MeiTuanNavViewItem(getContext());
            navViewItem.setNavData(navBean);
            final int finalI = i;
            navViewItem.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateStatus(finalI);
                }
            });
            navViewItem.setLayoutParams(itemLayoutParams);
            if(defaultPos == i){
                //等navViewItem测量完之后才执行选中动作
                navViewItem.post(new Runnable() {
                    @Override
                    public void run() {
                        navViewItem.setSelected(true);
                    }
                });
            }
            addView(navViewItem);
        }
    }

    /**
     * 更新状态
     * @param selectPos
     */
    private void updateStatus(int selectPos) {
        for(int i = 0 ; i < getChildCount() ; i ++){
            View child = getChildAt(i);
            if(child instanceof MeiTuanNavViewItem){
                MeiTuanNavViewItem navViewItem = (MeiTuanNavViewItem)child;
                navViewItem.setSelected(i == selectPos);
            }
        }
    }

    public static class NavBean{
        private String defaultImageUrl;//默认图片地址
        private String selectedImageUrl;//选中图片地址
        private Integer defaultImageRes;//默认图片资源id(优先调用)
        private Integer selectedImageRes;//选中图片资源id(优先调用)
        private String name;//tab名称
        private int defaultTxtColor = Color.GRAY;//默认文字的颜色
        private int selectedTxtColor = Color.BLACK;//选中文字的颜色

        public NavBean(Integer defaultImageRes, Integer selectedImageRes, String name) {
            this.defaultImageRes = defaultImageRes;
            this.selectedImageRes = selectedImageRes;
            this.name = name;
        }

        public int getDefaultTxtColor() {
            return defaultTxtColor;
        }

        public void setDefaultTxtColor(int defaultTxtColor) {
            this.defaultTxtColor = defaultTxtColor;
        }

        public int getSelectedTxtColor() {
            return selectedTxtColor;
        }

        public void setSelectedTxtColor(int selectedTxtColor) {
            this.selectedTxtColor = selectedTxtColor;
        }

        public String getDefaultImageUrl() {
            return defaultImageUrl;
        }

        public void setDefaultImageUrl(String defaultImageUrl) {
            this.defaultImageUrl = defaultImageUrl;
        }

        public String getSelectedImageUrl() {
            return selectedImageUrl;
        }

        public void setSelectedImageUrl(String selectedImageUrl) {
            this.selectedImageUrl = selectedImageUrl;
        }

        public Integer getDefaultImageRes() {
            return defaultImageRes;
        }

        public void setDefaultImageRes(int defaultImageRes) {
            this.defaultImageRes = defaultImageRes;
        }

        public Integer getSelectedImageRes() {
            return selectedImageRes;
        }

        public void setSelectedImageRes(int selectedImageRes) {
            this.selectedImageRes = selectedImageRes;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
